﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using Simple_HUD;


namespace PPP
{
    class Screen : PictureBox
    {

        #region /////Fields////////

        Rectangle AirplaneRec;
        Rectangle MotorRec;
        private float rollAngle = 0;
        private float angleOfAttack = 0;
        private float airspeed = 0;
        private float altitude = 0;
        private float pitchAngle = 0; 
        private float verticalVelocity = 0;
        private Font myFont = new Font("Times New Roman", 10);
        private  Font myFont2 = new Font("Times New Roman", 20);
        private Brush defmyBrush = new SolidBrush(Color.White);
        private Brush myBrush = new SolidBrush(Color.White);
        private Brush myBrush1 = new SolidBrush(Color.Red);
        private Brush myBrush2 = new SolidBrush(Color.Magenta);
        private Brush myBrush3 = new SolidBrush(Color.Black);
        private int colorNum = 0;
        private int Heading = 45;

        private int motorB = -45;
        private int motorL = 45;
        private int motorR = 45;

        public int MotorR
        {
            get { return motorR; }
            set { motorR = value; }
        }
        public int MotorL
        {
            get { return motorL; }
            set { motorL = value; }
        }
        
        public int MotorB
        {
            get { return motorB; }
            set { motorB = value; }
        }
        

        public int Heading1
        {
            get { return Heading; }
            set { Heading = value; }
        }


        private PointF myPoint = new PointF();
         private Pen myPen=System.Drawing.Pens.White;
         private bool enableHUD = true;

         public bool EnableHUD
         {
             get { return enableHUD; }
             set { enableHUD = value; }
         }

        public float RollAngle
        {
            get { return rollAngle; }
            set { rollAngle = value; }
        }
        

        public float AngleOfAttack
        {
            get { return angleOfAttack; }
            set { angleOfAttack = value; }
        }
       

        public float Airspeed
        {
            get { return airspeed; }
            set { airspeed = value; }
        }
     

        public float Altitude
        {
            get { return altitude; }
            set { altitude = value; }
        }
       

        public float PitchAngle
        {
            get { return pitchAngle; }
            set { pitchAngle = value; }
        }
      

        public float VerticalVelocity
        {
            get { return verticalVelocity; }
            set { verticalVelocity = value; }
        }
        
    

        #endregion
        
        public Screen()
        {
           SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

           RollAngle = 0;
           Airspeed = 0;
           Altitude = 0;
           PitchAngle = 0;
           VerticalVelocity = 0;
           AngleOfAttack = 0;
            

         AirplaneRec = new Rectangle(-5,-5, 10, 10);
         MotorRec = new Rectangle(0, 0, 150, 150);
                                


        }
        
        public void SetHudColor()
        {
            colorNum++;

            if (colorNum == 1)
            {
                myPen = System.Drawing.Pens.Red;
                defmyBrush = myBrush1;

            }
            else if (colorNum == 2)
            {
                myPen = System.Drawing.Pens.Magenta;
                defmyBrush = myBrush2;

            }
            else if (colorNum == 3)
            {
                myPen = System.Drawing.Pens.Black;
                defmyBrush = myBrush3;

            }
            else if (colorNum == 4)
            {
                myPen = System.Drawing.Pens.White;
                defmyBrush = myBrush;

            }


            if (colorNum == 4)
            {

                colorNum = 0;

            }



        }
        
        public void SetVariables(float VerticalVel, float AirSpd,float PitchAngl,float Alt,float RollAngl )
        {

           RollAngle = RollAngl;
           Airspeed = AirSpd;
           Altitude = Alt;
           PitchAngle = PitchAngl;
           VerticalVelocity = VerticalVel;


            float VelocityHorizon = Airspeed * (float)Math.Cos(PitchAngle * (Math.PI / 180));

            float FlightPathAngleFromHorizon =   (float)Math.Atan(VerticalVelocity/ VelocityHorizon)  * (float)(180/Math.PI)   ;

            AngleOfAttack = PitchAngle - FlightPathAngleFromHorizon;


            if (float.NaN.Equals(AngleOfAttack))
            {

                AngleOfAttack = 0;

            }




            

        }
                     
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            if (EnableHUD == true)
            {


                //  ////////////////////////////Cross/////////////////////////////////////////////////////////////////
                

                //pe.Graphics.ResetTransform();
                //pe.Graphics.TranslateTransform(this.Width / 2 , this.Height / 2 - 150);


                //pe.Graphics.DrawLine(myPen, -7, 0, 7, 0);
                //pe.Graphics.DrawLine(myPen, 0, -7, 0, 7);

                //  /////////////////////////////Airplane//////////////////////////////////////////////////////////         


                pe.Graphics.ResetTransform();


                pe.Graphics.TranslateTransform(this.Width / 2 , this.Height / 2 - 5 - 150);


                pe.Graphics.TranslateTransform(0, (AngleOfAttack * 100) / 5);

                pe.Graphics.DrawArc(myPen, AirplaneRec, 0, 360);
                pe.Graphics.DrawLine(myPen, AirplaneRec.X, AirplaneRec.Y + 5, AirplaneRec.X - 15, AirplaneRec.Y + 5);
                pe.Graphics.DrawLine(myPen, AirplaneRec.X + AirplaneRec.Width, AirplaneRec.Y + 5, AirplaneRec.X + AirplaneRec.Width + 15, AirplaneRec.Y + 5);
                pe.Graphics.DrawLine(myPen, AirplaneRec.X + AirplaneRec.Width / 2, AirplaneRec.Y, AirplaneRec.X + AirplaneRec.Width / 2, AirplaneRec.Y - 10);

                pe.Graphics.ResetTransform();

                myPoint.X = 20;
                myPoint.Y = 20;
                pe.Graphics.DrawString("Roll Angle:  " + rollAngle, myFont, defmyBrush, myPoint);

                //myPoint.X = -45;
                //myPoint.Y = -5;
                //pe.Graphics.DrawString("" + AngleOfAttack, myFont, defmyBrush, myPoint);


                //////////////////////////////////Compas//////////////////////////////////////////////////////////

                pe.Graphics.ResetTransform();
                pe.Graphics.TranslateTransform(this.Width -150, 100);

                int xcenter = 0;
               int ycenter = 0;
                int radiusInner = 80;
               
                int radius = 100;
               

                // draw separator lines
                for (int l = 0; l < 16; l++)
                {
                    double angle = l * 22.5 + Heading;
                    double xout = Math.Sin(angle *(Math.PI/180)) * radiusInner;
                    double yout = Math.Cos(angle * (Math.PI / 180)) * radiusInner;
                    double xin = Math.Sin(angle * (Math.PI / 180)) * (radiusInner - 2 * this.Font.Size/2);
                    double yin = Math.Cos(angle * (Math.PI / 180)) * (radiusInner - 2 * this.Font.Size/2);
                  
                pe.Graphics.DrawLine(myPen, (int)xout + xcenter, (int)yout + ycenter, (int)xin + xcenter, (int)yin + ycenter);

                }

                // draw N, W, E, S
                double x = Math.Sin(Heading * (Math.PI / 180)) * radius;
                double y = Math.Cos(Heading * (Math.PI / 180)) * radius;
                pe.Graphics.DrawString("N", myFont2, defmyBrush, (float)(xcenter - x) - myFont2.Size / 2, (float)(ycenter - y) - myFont2.Size / 2);
                pe.Graphics.DrawString("S", myFont2, defmyBrush, (float)(xcenter + x) - myFont2.Size / 2, (float)(ycenter + y) - myFont2.Size / 2);
                pe.Graphics.DrawString("W", myFont2, defmyBrush, (float)(xcenter - y) - myFont2.Size / 2, (float)(ycenter + x) - myFont2.Size / 2);
                pe.Graphics.DrawString("E", myFont2, defmyBrush, (float)(xcenter + y) - myFont2.Size / 2, (float)(ycenter - x) - myFont2.Size / 2);

                // draw needle
                pe.Graphics.DrawLine(myPen, xcenter, ycenter, xcenter,  - 50);            

                // draw digital heading
                myPoint.X = xcenter-20;
                myPoint.Y = ycenter+10;
                pe.Graphics.DrawString("" + Heading, myFont2, defmyBrush, myPoint);





                //////////////////////////////////RollAngle//////////////////////////////////////////////////////////

                pe.Graphics.ResetTransform();
                pe.Graphics.TranslateTransform(this.Width / 2 , this.Height / 2 - 150);
                pe.Graphics.TranslateTransform(0, (AngleOfAttack * 100) / 5);//100px/g degrees = x/PitchAngle       
                pe.Graphics.RotateTransform(RollAngle);

                pe.Graphics.DrawLine(myPen, 0, -30, 0, -40);
                pe.Graphics.RotateTransform(45);
                pe.Graphics.DrawLine(myPen, 0, -30, 0, -40);
                pe.Graphics.RotateTransform(-90);
                pe.Graphics.DrawLine(myPen, 0, -30, 0, -40);




                ///////////////////////////////////Horizontals/////////////////////////////////////////////////////////


                pe.Graphics.ResetTransform();
                pe.Graphics.TranslateTransform(this.Width / 2 , this.Height / 2 - 150);
                pe.Graphics.TranslateTransform(0, (AngleOfAttack * 100) / 5);//100px/g degrees = x/PitchAngle       
                pe.Graphics.RotateTransform(RollAngle);
                pe.Graphics.TranslateTransform(0, ((PitchAngle - AngleOfAttack) * 100) / 5);


                int internalforloopPitch = 0;

                pe.Graphics.DrawLine(myPen, 40, 0, 300, 0);
                pe.Graphics.DrawLine(myPen, -40, 0, -300, 0);


                internalforloopPitch -= 5;

                for (int i = 1; i <= 18; i++)
                {

                    pe.Graphics.DrawLine(myPen, 50, 100 * i, 100, 100 * i);
                    pe.Graphics.DrawLine(myPen, 100, 100 * i, 100, 15 + 100 * i);
                    myPoint.X = 110;
                    myPoint.Y = 100 * i;
                    pe.Graphics.DrawString("" + internalforloopPitch, myFont, defmyBrush, myPoint);

                    pe.Graphics.DrawLine(myPen, -50, 100 * i, -100, 100 * i);
                    pe.Graphics.DrawLine(myPen, -100, 100 * i, -100, 15 + 100 * i);
                    myPoint.X = -130;
                    myPoint.Y = 100 * i;
                    pe.Graphics.DrawString("" + internalforloopPitch, myFont, defmyBrush, myPoint);

                    internalforloopPitch -= 5;
                }

                internalforloopPitch = 0;

                internalforloopPitch += 5;

                for (int i = 1; i <= 18; i++)
                {


                    pe.Graphics.DrawLine(myPen, 50, -100 * i, 100, -100 * i);
                    pe.Graphics.DrawLine(myPen, 100, -100 * i, 100, 15 + -100 * i);
                    myPoint.X = 110;
                    myPoint.Y = -100 * i;
                    pe.Graphics.DrawString("" + internalforloopPitch, myFont, defmyBrush, myPoint);

                    pe.Graphics.DrawLine(myPen, -50, -100 * i, -100, -100 * i);
                    pe.Graphics.DrawLine(myPen, -100, -100 * i, -100, 15 - 100 * i);
                    myPoint.X = -130;
                    myPoint.Y = -100 * i;
                    pe.Graphics.DrawString("" + internalforloopPitch, myFont, defmyBrush, myPoint);



                    internalforloopPitch += 5;
                }

                //////////////////////////Airspeed////////////////////////////////////////////////////////////

                pe.Graphics.ResetTransform();

                pe.Graphics.TranslateTransform(0, this.Height / 2);
                pe.Graphics.DrawRectangle(myPen, 15, -13, 70, 26);
                myPoint.X = 25;
                myPoint.Y = -14;
                pe.Graphics.DrawString("" + airspeed, myFont2, defmyBrush, myPoint);


                pe.Graphics.ResetTransform();

                pe.Graphics.TranslateTransform(100, this.Height / 2);

                pe.Graphics.DrawLine(myPen, -10, 0, 15, 0);


                int internalforloopAirspeed = (int)airspeed + 1;
                for (int i = 1; i <= 15; i++)
                {
                    if (internalforloopAirspeed % 10 == 0)
                    {
                        pe.Graphics.DrawLine(myPen, 0, -10 * i, 15, -10 * i);
                        myPoint.X = 25;
                        myPoint.Y = (-10 * i) - 6;
                        pe.Graphics.DrawString("" + internalforloopAirspeed, myFont, defmyBrush, myPoint);
                    }
                    else if (internalforloopAirspeed % 5 == 0)
                    {
                        pe.Graphics.DrawLine(myPen, 0, -10 * i, 10, -10 * i);
                        myPoint.X = 25;
                        myPoint.Y = (-10 * i) - 6;
                        pe.Graphics.DrawString("" + internalforloopAirspeed, myFont, defmyBrush, myPoint);
                    }
                    else
                    {
                        pe.Graphics.DrawLine(myPen, 0, -10 * i, 5, -10 * i);

                    }

                    internalforloopAirspeed++;

                }

                internalforloopAirspeed = (int)airspeed - 1;

                for (int i = 1; i <= 15; i++)
                {
                    if (internalforloopAirspeed % 10 == 0)
                    {
                        pe.Graphics.DrawLine(myPen, 0, 10 * i, 15, 10 * i);
                        myPoint.X = 25;
                        myPoint.Y = (10 * i) - 6;
                        pe.Graphics.DrawString("" + internalforloopAirspeed, myFont, defmyBrush, myPoint);

                    }
                    else if (internalforloopAirspeed % 5 == 0)
                    {
                        pe.Graphics.DrawLine(myPen, 0, 10 * i, 10, 10 * i);
                        myPoint.X = 25;
                        myPoint.Y = (10 * i) - 6;
                        pe.Graphics.DrawString("" + internalforloopAirspeed, myFont, defmyBrush, myPoint);
                    }
                    else
                    {
                        pe.Graphics.DrawLine(myPen, 0, 10 * i, 5, 10 * i);


                    }

                    internalforloopAirspeed--;
                }



                ///////////////////////////////////Altitude/////////////////////////////////////////////////////////

                pe.Graphics.ResetTransform();

                pe.Graphics.TranslateTransform(this.Width, this.Height / 2);
                pe.Graphics.DrawRectangle(myPen, -85, -13, 70, 26);
                myPoint.X = -75;
                myPoint.Y = -14;
                pe.Graphics.DrawString("" + altitude, myFont2, defmyBrush, myPoint);







                pe.Graphics.ResetTransform();

                pe.Graphics.TranslateTransform(this.Width - 100, this.Height / 2);

                pe.Graphics.DrawLine(myPen, -15, 0, 10, 0);


                int internalforloopAltitude = (int)altitude + 1;
                for (int i = 1; i <= 15; i++)
                {
                    if (internalforloopAltitude % 10 == 0)
                    {

                        pe.Graphics.DrawLine(myPen, 0, -10 * i, -15, -10 * i);
                        myPoint.X = -45;
                        myPoint.Y = (-10 * i) - 6;
                        pe.Graphics.DrawString("" + internalforloopAltitude, myFont, defmyBrush, myPoint);
                    }
                    else if (internalforloopAltitude % 5 == 0)
                    {
                        pe.Graphics.DrawLine(myPen, 0, -10 * i, -10, -10 * i);
                        myPoint.X = -45;
                        myPoint.Y = (-10 * i) - 6;
                        pe.Graphics.DrawString("" + internalforloopAltitude, myFont, defmyBrush, myPoint);
                    }
                    else
                    {
                        pe.Graphics.DrawLine(myPen, 0, -10 * i, -5, -10 * i);

                    }

                    internalforloopAltitude++;

                }

                internalforloopAltitude = (int)altitude - 1;

                for (int i = 1; i <= 15; i++)
                {
                    if (internalforloopAltitude % 10 == 0)
                    {
                        pe.Graphics.DrawLine(myPen, 0, 10 * i, -15, 10 * i);
                        myPoint.X = -45;
                        myPoint.Y = (10 * i) - 6;
                        pe.Graphics.DrawString("" + internalforloopAltitude, myFont, defmyBrush, myPoint);
                    }
                    else if (internalforloopAltitude % 5 == 0)
                    {
                        pe.Graphics.DrawLine(myPen, 0, 10 * i, -10, 10 * i);
                        myPoint.X = -45;
                        myPoint.Y = (10 * i) - 6;
                        pe.Graphics.DrawString("" + internalforloopAltitude, myFont, defmyBrush, myPoint);
                    }
                    else
                    {
                        pe.Graphics.DrawLine(myPen, 0, 10 * i, -5, 10 * i);

                    }

                    internalforloopAltitude--;
                }

                /////////////////////////////////InFo////////////////////////////////////////////////////////////////////


                pe.Graphics.ResetTransform();

                pe.Graphics.TranslateTransform(70, this.Height - 200);
                myPoint.X = 0;
                myPoint.Y = 0;
                pe.Graphics.DrawString("Waypoint: " + 4, myFont, defmyBrush, myPoint);
                myPoint.X = 0;
                myPoint.Y = 20;
                pe.Graphics.DrawString("  Distance: " + 10, myFont, defmyBrush, myPoint);
                myPoint.X = 0;
                myPoint.Y = 40;
                pe.Graphics.DrawString("       Time: " + 14, myFont, defmyBrush, myPoint);


                myPoint.X = 0;
                myPoint.Y = 80;
                pe.Graphics.DrawString("      Power: ", myFont, defmyBrush, myPoint);
                myPoint.X = -28;
                myPoint.Y = 120;
                pe.Graphics.DrawString("Signal Strength: ", myFont, defmyBrush, myPoint);

                pe.Graphics.DrawRectangle(myPen, 65, 78, 100, 20);
                pe.Graphics.FillRectangle(defmyBrush, 65, 78, 50, 20);

                pe.Graphics.DrawRectangle(myPen, 65, 120, 100, 20);
                pe.Graphics.FillRectangle(defmyBrush, 65, 120, 70, 20);






                /////////////////////////////////Motors////////////////////////////////////////////////////////////////////
                pe.Graphics.ResetTransform();

                // Bottom 
                pe.Graphics.ResetTransform();
                pe.Graphics.TranslateTransform(this.Width - 242, this.Height - 150);
                pe.Graphics.RotateTransform(-30);
                pe.Graphics.DrawPie(myPen, MotorRec, 0, -1 * ((120 * Math.Abs(MotorB)) / 100));
                pe.Graphics.RotateTransform(30);
                myPoint.X = 80;
                myPoint.Y = -25;
                pe.Graphics.DrawString("B: " + MotorB + " %", myFont, defmyBrush, myPoint);


                //Right
                pe.Graphics.ResetTransform();
                pe.Graphics.TranslateTransform(this.Width - 60, this.Height - 190);
                pe.Graphics.RotateTransform(90);
                pe.Graphics.DrawPie(myPen, MotorRec, 0, -1 * ((120 * Math.Abs(MotorR)) / 100));
                pe.Graphics.RotateTransform(-90);
                myPoint.X = -70;
                myPoint.Y = 85;
                pe.Graphics.DrawString("R: " + MotorR + " %", myFont, defmyBrush, myPoint);

                //Left
                pe.Graphics.ResetTransform();
                pe.Graphics.TranslateTransform(this.Width - 70, this.Height - 190);
                pe.Graphics.RotateTransform(90);
                pe.Graphics.DrawPie(myPen, MotorRec, 0, ((120 * Math.Abs(MotorL)) / 100));
                pe.Graphics.RotateTransform(-90);
                myPoint.X = -140;
                myPoint.Y = 85;
                pe.Graphics.DrawString("L: " + MotorL + " %", myFont, defmyBrush, myPoint);




            }

        }

        

    }
}
